# coding=utf-8
__author__ = 'Fonny'


def mariaDBTest():
	import pymysql.cursors

	# 需要手动创建数据库,见 '创建测试表.sql'
	# Connect to the database
	connection = pymysql.connect(host='localhost',
	                             user='root',
	                             password='123',
	                             db='test',
	                             charset='utf8mb4',
	                             cursorclass=pymysql.cursors.DictCursor)

	try:
		with connection.cursor() as cursor:
			# Create a new record
			sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
			cursor.execute(sql, ('中文字符串4@python.org', '密码是刘德华'))

		# connection is not autocommit by default. So you must commit to save
		# your changes.
		connection.commit()

		with connection.cursor() as cursor:
			# Read a single record
			sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
			cursor.execute(sql, ('中文字符串4@python.org',))

			result = cursor.fetchone()
			print(result)
			print(type(result))
			print(result['password'])
			print(type(result['password']))
	finally:
		connection.close()

def main():
	mariaDBTest()


if __name__ == '__main__':
	main()
